package com.qinyunkj.searchchatserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qinyunkj.searchchatserver.mapper.ArticleMapper;
import com.qinyunkj.searchchatserver.mapper.ArticleQAMapper;
import com.qinyunkj.searchchatserver.model.dto.ArticleQAQuery;
import com.qinyunkj.searchchatserver.model.entity.Article;
import com.qinyunkj.searchchatserver.model.entity.ArticleQA;
import com.qinyunkj.searchchatserver.model.vo.ArticleQAStatistics;
import com.qinyunkj.searchchatserver.model.vo.ArticleQAVO;
import com.qinyunkj.searchchatserver.service.ArticleQAService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ArticleQAServiceImpl implements ArticleQAService {

    private final ArticleQAMapper articleQAMapper;

    private final ArticleMapper articleMapper;

    @Override
    public IPage<ArticleQAVO> getQAPage(ArticleQAQuery query) {
        Page<ArticleQAVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return articleQAMapper.selectQAPage(page, query);
    }

    @Override
    public ArticleQA getById(Long id) {
        return articleQAMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ArticleQA articleQA) {
        // 参数校验
        if (StringUtils.isBlank(articleQA.getQuestion())) {
            throw new IllegalArgumentException("问题不能为空");
        }
        if (StringUtils.isBlank(articleQA.getAnswer())) {
            throw new IllegalArgumentException("答案不能为空");
        }
        if (articleQA.getSceneType() == null) {
            throw new IllegalArgumentException("场景类型不能为空");
        }
        // 如果有文章ID，验证文章是否存在
        if (articleQA.getArticleId() != null) {
            Article article = articleMapper.selectById(articleQA.getArticleId());
            if (article == null) {
                throw new IllegalArgumentException("关联的文章不存在");
            }
        }

        articleQAMapper.insert(articleQA);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ArticleQA articleQA) {
        // 参数校验
        if (articleQA.getId() == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        if (StringUtils.isBlank(articleQA.getQuestion())) {
            throw new IllegalArgumentException("问题不能为空");
        }
        if (StringUtils.isBlank(articleQA.getAnswer())) {
            throw new IllegalArgumentException("答案不能为空");
        }
        if (articleQA.getSceneType() == null) {
            throw new IllegalArgumentException("场景类型不能为空");
        }
        // 如果有文章ID，验证文章是否存在
        if (articleQA.getArticleId() != null) {
            Article article = articleMapper.selectById(articleQA.getArticleId());
            if (article == null) {
                throw new IllegalArgumentException("关联的文章不存在");
            }
        }

        articleQAMapper.updateById(articleQA);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        articleQAMapper.deleteById(id);
    }

    @Override
    public ArticleQAStatistics getStatistics() {
        return articleQAMapper.selectQAStatistics();
    }
}
